﻿
Partial Class pruebas_SubirArchivo
    Inherits System.Web.UI.Page

    Protected Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UploadButton.Click
        ' Especificar la ruta del servidor
        ' donde se guardará el archivo
        Dim savePath As String = "C:\Users\rledesma\Documents\Santafe\photos\"

        ' Antes de intentar realizar operaciones
        ' sobre el rachivo, verificar que el control
        ' FileUpload contiene un archivo.

        If (FileUpload1.HasFile) Then
            ' Obtener el nombre del archivo a subir.
            Dim fileName As String = FileUpload1.FileName

            ' Añadir el nombre del archivo a subir a la ruta.
            savePath += fileName

            ' Call the SaveAs method to save the 
            ' uploaded file to the specified path.
            ' This example does not perform all
            ' the necessary error checking.               
            ' If a file with the same name
            ' already exists in the specified path,  
            ' the uploaded file overwrites it.
            FileUpload1.SaveAs(savePath)

            ' Call a helper routine to display the contents
            ' of the file to upload.
            DisplayFileContents(FileUpload1.PostedFile)


            ' Notify the user of the name the file
            ' was saved under.
            UploadStatusLabel.Text = "Your file was saved as " & fileName

        Else
            ' Notify the user that a file was not uploaded.
            UploadStatusLabel.Text = "You did not specify a file to upload."
        End If


    End Sub

    Sub DisplayFileContents(ByVal file As HttpPostedFile)

        Dim myStream As System.IO.Stream
        Dim fileLen As Integer
        Dim displayString As New StringBuilder()
        Dim loop1 As Integer

        ' Get the length of the file.
        fileLen = FileUpload1.PostedFile.ContentLength

        ' Display the length of the file in a label.
        LengthLabel.Text = "The length of the file is " _
                           + fileLen.ToString + " bytes."

        ' Create a byte array to hold the contents of the file.
        Dim Input(fileLen) As Byte

        ' Initialize the stream to read the uploaded file.
        myStream = FileUpload1.FileContent

        ' Read the file into the byte array.
        myStream.Read(Input, 0, fileLen)

        ' Copy the byte array to a string.
        For loop1 = 0 To fileLen - 1
            displayString.Append(Input(loop1).ToString())
        Next loop1

        ' Display the contents of the file in a 
        ' textbox on the page.
        ContentsLabel.Text = "The contents of the file as bytes:"

        Dim ContentsTextBox As New TextBox
        ContentsTextBox.TextMode = TextBoxMode.MultiLine
        ContentsTextBox.Height = Unit.Pixel(300)
        ContentsTextBox.Width = Unit.Pixel(400)
        ContentsTextBox.Text = displayString.ToString()

        ' Add the textbox to the Controls collection
        ' of the Placeholder control.
        PlaceHolder1.Controls.Add(ContentsTextBox)

    End Sub

End Class
